package com.example.mylogin;

import static android.telephony.SubscriptionManager.DEFAULT_SUBSCRIPTION_ID;

import android.Manifest;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.os.Looper;
import android.telephony.SubscriptionManager;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.constraintlayout.helper.widget.CircularFlow;
import androidx.core.app.ActivityCompat;

import com.google.android.material.chip.Chip;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class MainActivity extends AppCompatActivity {
    Connection conn = null;
    Statement stmt = null;
    ResultSet rs = null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        getPhoneNumber();
    }
    void getPhoneNumber(){
        SubscriptionManager subscriptionManager = (SubscriptionManager) getSystemService(SubscriptionManager.class);
        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_PHONE_NUMBERS) != PackageManager.PERMISSION_GRANTED) {
            // TODO: Consider calling
            //    ActivityCompat#requestPermissions
            // here to request the missing permissions, and then overriding
            //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
            //                                          int[] grantResults)
            // to handle the case where the user grants the permission. See the documentation
            // for ActivityCompat#requestPermissions for more details.
            return;
        }
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
            String phoneNumber=subscriptionManager.getPhoneNumber(DEFAULT_SUBSCRIPTION_ID);
            TextView textView=(TextView)findViewById(R.id.phoneNumber);
            textView.setText(phoneNumber);
        }
    }
    void getConnection() throws ClassNotFoundException {
        //Class.forName("com.mysql.cj.jdbc.Driver");

        //Class.forName("com.mysql.jdbc.Driver");
        Class.forName("java.sql.SQLType");
       /* try {
            conn = DriverManager.getConnection("jdbc:mysql://10.100.20.8/menagerie?" + "user=fengying&password=123456pw");
            System.out.println("conn.isClosed: " +conn.isClosed() );
            select();
        } catch (SQLException ex) {
            System.out.println("SQLException: " + ex.getMessage());
            System.out.println("SQLState: " + ex.getSQLState());
            System.out.println("VendorError: " + ex.getErrorCode());
        }*/
    }
    public void select() {
        try {
            stmt = conn.createStatement();
            //rs = stmt.executeQuery("SELECT name FROM pet");
            if (stmt.execute("SELECT name, index_1 FROM pet")) {
                // if (stmt.execute("SELECT * FROM pet")) {
                rs = stmt.getResultSet();
                while (rs.next()) {

                    int rows= rs.getRow();

                    String  name=rs.getString(1);

                    int  index=rs.getInt(2);
                    System.out.println("rows: " + rows);
                    System.out.println("name: " + name);
                    System.out.println("index: " +index);
                }


            }}	catch (SQLException ex){
            // handle any errors
            System.out.println("SQLException: " + ex.getMessage());
            System.out.println("SQLState: " + ex.getSQLState());
            System.out.println("VendorError: " + ex.getErrorCode());
        }
    }
    @SuppressLint("IntentReset")
    public void getConnection(View view){
        Log.d(getLocalClassName()+":", "getConnection");

        CircularFlow circularFlow=new CircularFlow(getApplicationContext());
        ImageView imageView=((ImageView)findViewById(R.id.imageView));
        imageView.
        circularFlow.addViewToCircularFlow(imageView,12,45);

        /*new Thread(new Runnable() {
            public void run() {
                Looper.prepare();
                try {
                    getConnection();
                } catch (ClassNotFoundException e) {

                    Log.d(getLocalClassName()+":", String.valueOf(e.getCause()));
                    Log.d(getLocalClassName()+":", e.getMessage());
                    Log.d(getLocalClassName()+":",e.getLocalizedMessage() );
                    throw new RuntimeException(e);
                }
                Looper.loop();
            }
        }).start();*/
    }
}